package com.wwp.wkb.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwp.wkb.R;
import com.wwp.wkb.base.impl.BaseViewDefault;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment implements BaseView {
    public View mView;
    public Context context;
    public BaseView baseView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //ViewPager时，先执行setUserVisibleHint（在onAttach之前执行）；每次切换tab，都会执行一次setUserVisibleHint()方法
        if (mView == null) {
            mView = inflater.inflate(setFragmentLayoutId(), container, false);
            ButterKnife.bind(this, mView);
            context = getContext();
            baseView = createLoading(context);
            setUpView();
        }
        return mView;
    }

    protected abstract int setFragmentLayoutId();

    protected abstract void setUpView();

    protected BaseView createLoading(Context context) {
        return new BaseViewDefault(context);
    }

    public void setBack() {
        ImageView ivBack = (ImageView) mView.findViewById(R.id.iv_back);
        if (ivBack != null) {
            ivBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getActivity().finish();
                }
            });
        }
    }

    public void setBackGone() {
        ImageView iv_back = (ImageView) mView.findViewById(R.id.iv_back);
        if (iv_back != null) {
            iv_back.setVisibility(View.GONE);
        }
    }

    public void setTitleBarTitle(String title) {
        TextView tvTitle = (TextView) mView.findViewById(R.id.tv_title);
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    public void setTitleBarTitle(int title) {
        TextView tvTitle = (TextView) mView.findViewById(R.id.tv_title);
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }
}
